<?php
class Auth_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        if ( ! $this -> session -> userdata('logged_in'))
        {
            redirect('login','refresh');
        }
        else
        {
            $session_data = $this -> session -> userdata('logged_in');
            $this -> data['username'] = $session_data['username'];
            $this -> data['kode_privilege'] = $session_data['kode_privilege'];
            $this -> data['kode_info_cabang'] = $session_data['kode_info_cabang'];
            $this -> data['kode_member'] = $session_data['kode_member'];
            $this -> data['kode_unit'] = $session_data['kode_unit'];
            $this -> data['kode_upline'] = $session_data['kode_upline'];
            $this -> data['active'] = $session_data['active'];
            $this -> data['cabang'] = $session_data['cabang'];
        }
        foreach ($this -> data['cabang'] as $row) {
            echo $row['database_cabang'];
            $db['cabang']['hostname'] = 'localhost';
            $db['cabang']['username'] = 'tulip';
            $db['cabang']['password'] = 'weareone';
            $db['cabang']['database'] = 'twintulipware_admin';
            $db['cabang']['dbdriver'] = 'mysql';
            $db['cabang']['dbprefix'] = '';
            $db['cabang']['pconnect'] = FALSE;
            $db['cabang']['db_debug'] = TRUE;
            $db['cabang']['cache_on'] = FALSE;
            $db['cabang']['cachedir'] = '';
            $db['cabang']['char_set'] = 'utf8';
            $db['cabang']['dbcollat'] = 'utf8_general_ci';
            $db['cabang']['swap_pre'] = '';
            $db['cabang']['autoinit'] = TRUE;
            $db['cabang']['stricton'] = FALSE;

            $DB_cabang[$row['kode_cabang']] = $this -> load -> database($db['cabang'], TRUE);
        }
    }
}

class Level_0_Auth extends Auth_Controller {

    function __construct()
    {
        parent::__construct();
        $session_data = $this -> session -> userdata('logged_in');
        if ($session_data['kode_privilege'] != 0)
        {
            show_error('You are not authorized to view this page');
        }
    }
}

class Level_1_Auth extends Auth_Controller {

    function __construct()
    {
        parent::__construct();
        $session_data = $this -> session -> userdata('logged_in');
        if ($session_data['kode_privilege'] > 1)
        {
            show_error('You are not authorized to view this page');
        }
    }
}

class Level_2_Auth extends Auth_Controller {

    function __construct()
    {
        parent::__construct();
        $session_data = $this -> session -> userdata('logged_in');
        if ($session_data['kode_privilege'] > 2)
        {
            show_error('You are not authorized to view this page');
        }
    }
}

class Level_3_Auth extends Auth_Controller {

    function __construct()
    {
        parent::__construct();
        $session_data = $this -> session -> userdata('logged_in');
        if ($session_data['kode_privilege'] > 3)
        {
            show_error('You are not authorized to view this page');
        }
    }
}
?>